﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Data;

namespace SES.CMS.Module
{
    public partial class ucLeftMenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadMoreInfo();

            }
            if (Request.QueryString["Page"] != null)
            {
                if (Request.QueryString["Page"].ToString() != "LibsFile")
                {
                    getCate();
                }
            }
        }
        protected void getCate()
        {
            string curUrl = Request.Url.AbsolutePath;
            string cutCurUrl = curUrl.Substring(1, curUrl.Length - 1);
            string cutCurUrl1 = cutCurUrl.Substring(cutCurUrl.IndexOf("/") + 1);

            string Module = cutCurUrl1.Substring(0, cutCurUrl1.IndexOf("/"));
            if (Module.Equals("Category"))
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    int categoryID = int.Parse(Request.QueryString["ID"]);
                    cmsCategoryDO objCate = new cmsCategoryDO() { CategoryID = categoryID };
                    objCate = new cmsCategoryBL().Select(objCate);
                    if (objCate.ParentID == 0)
                    {
                        rptCateMenu.Visible = true;
                        rptArtMenu.Visible = false;
                        rptCateMenu.DataSource = new cmsCategoryBL().SelectAllByParent(categoryID);
                        rptCateMenu.DataBind();
                    }
                    else
                    {
                        rptCateMenu.Visible = true;
                        rptArtMenu.Visible = false;
                        rptCateMenu.DataSource = new cmsCategoryBL().SelectAllByParent(objCate.ParentID);
                        rptCateMenu.DataBind();
                    }
                }
            }
            else  if ( Module.Equals("Articles"))
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    int categoryID = int.Parse(Request.QueryString["ID"]);
                    cmsCategoryDO objCate = new cmsCategoryDO() { CategoryID = categoryID };
                    objCate = new cmsCategoryBL().Select(objCate);
                    if (objCate.ParentID == 0)
                    {
                        rptCateMenu.Visible = false;
                        rptArtMenu.Visible = true;
                        rptArtMenu.DataSource = new cmsCategoryBL().SelectAllByParent(categoryID);
                        rptArtMenu.DataBind();
                    }
                    else
                    {
                        rptCateMenu.Visible = false;
                        rptArtMenu.Visible = true;
                        rptArtMenu.DataSource = new cmsCategoryBL().SelectAllByParent(objCate.ParentID);
                        rptArtMenu.DataBind();
                    }
                }
            }
        }
        protected void rptCateMenu_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string curUrl = Request.Url.AbsolutePath;
                string cutCurUrl = curUrl.Substring(1, curUrl.Length - 1);
                string cutCurUrl1 = cutCurUrl.Substring(cutCurUrl.IndexOf("/") + 1);

                string Module = cutCurUrl1.Substring(0, cutCurUrl1.IndexOf("/"));
                if (Module.Equals("Category"))
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                    {
                        int categoryID = int.Parse(Request.QueryString["ID"]);
                        Panel divCate = (Panel)e.Item.FindControl("divCate");
                        int cateID = int.Parse(((DataRowView)e.Item.DataItem).Row["CategoryID"].ToString());
                        if (categoryID == cateID)
                        {
                            divCate.CssClass = "l-menu-cate-active l-menu-cate";
                        }
                    }
                }
            }
        }
        protected void rptArtMenu_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string curUrl = Request.Url.AbsolutePath;
                string cutCurUrl = curUrl.Substring(1, curUrl.Length - 1);
                string cutCurUrl1 = cutCurUrl.Substring(cutCurUrl.IndexOf("/") + 1);

                string Module = cutCurUrl1.Substring(0, cutCurUrl1.IndexOf("/"));
                if (Module.Equals("Articles"))
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                    {
                        int categoryID = int.Parse(Request.QueryString["ID"]);
                        Panel divCate = (Panel)e.Item.FindControl("divCate");
                        int cateID = int.Parse(((DataRowView)e.Item.DataItem).Row["CategoryID"].ToString());
                        if (categoryID == cateID)
                        {
                            divCate.CssClass = "l-menu-cate-active l-menu-cate";
                        }
                    }
                }
            }
        }
        protected string ReturnLink()
        {
            string preLink = "";
            string curUrl = Request.Url.AbsolutePath;
            string cutCurUrl = curUrl.Substring(1, curUrl.Length - 1);
            preLink = cutCurUrl.Substring(0, cutCurUrl.IndexOf("/"));
            return preLink;
        }
        protected void loadMoreInfo()
        {
            sysConfigDO objConfig = new sysConfigDO();
            sysConfigBL configBL = new sysConfigBL();

            objConfig.ConfigID = 8;
            objConfig = configBL.Select(objConfig);
            ltrInfo1.Text = objConfig.ConfigName;
            ltrInfo1x.Text = objConfig.ConfigValue;

            objConfig.ConfigID = 9;
            objConfig = configBL.Select(objConfig);
            ltrInfo2.Text = objConfig.ConfigName;
            ltrInfo2x.Text = objConfig.ConfigValue;

            objConfig.ConfigID = 10;
            objConfig = configBL.Select(objConfig);
            ltrInfo3.Text = objConfig.ConfigName;
            ltrInfo3x.Text = objConfig.ConfigValue;
        }

        protected string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
    }
}